﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcContrib.ActionResults;
using Press.MVC.Services;
using System.Xml;


namespace Press.MVC.Web.Controllers
{
    public class WebInstallController : BaseController
    {
        IDBUtilService _dbUtilService;
        IUserService _userService;

        public WebInstallController(IDBUtilService dbUtilService, IUserService userService)
        {
            _dbUtilService = dbUtilService;
            _userService = userService;
        }

        public ActionResult Index()
        {
            // Check if the user has already run the Installer, else run the installer
            string errorMessage = "";
            if (_dbUtilService.IsInstalled())
            {
                ViewData["IsInstalled"] = "true";
            }
            else
            {
                ViewData["IsInstalled"] = "false";
            }

            ViewData["appconfigConnectionString"] = _dbUtilService.GetConnectionString();

            return View("Step1");
        }

        public ActionResult Step2()
        {
            return View("Step2");
        }

        public ActionResult InstallDB()
        {
            //1) Create Database with Linq Classes
            //2) Create ASP.NET Membership Tables/Views/SPs
            //3) Create an entry for MVC Press in ASP.NET Application table with aspnet_Applications_CreateApplication SP
            //4) Add MVC Press Roles "MtaAdmin", "BlogAdmin"
            bool installed = _dbUtilService.InstallDB();

            //TODO error checking. If any of the 3 steps above fail we need to pass these errors from the Data Layer
            // back to the View and display to the user.
            return RedirectToAction("Step3");
        }

        public ActionResult Step3()
        {
            string errorMessage = "";
            // Verify the System is installed. 
            // TODO change this so that it runs IsInstalled and checks for data in the Application table for an application name with "mvcpress"
            ViewData["isValidDbConn"] = _dbUtilService.CanVerifyDatabaseConnection(out errorMessage);

            ViewData["dbErrorMessage"] = errorMessage;
            return View("Step3");
        }

        public ActionResult CreateGlobalAdmin()
        {
            // Add the Global Host Administrator into the system and to the MtaAdmin Role
            _userService.CreateMtaAdminUser(this.ReadFromRequest("username"), this.ReadFromRequest("password"), this.ReadFromRequest("passwordagain"), this.ReadFromRequest("email"), "mvcpress", "mvcpress");

            // Send the user to the Mta Page
            return RedirectToAction("Index", "Mta");
        }

        public ActionResult ReInstallDb()
        {
            //_dbUtilService.ReInstallDB();
            return RedirectToAction("Index");
        }

    }
}
